<template>
  <page-card title="公告管理">
    <div class="table-page-search-wrapper">
      <a-row type="flex"
             style="margin-bottom:20px"
             justify="end">
        <a-col v-if="hasPermission('add')"
               flex="1">
          <a-button type="primary"
                    icon="plus"
                    action
                    @click="handleEdit()">
            新增
          </a-button>
        </a-col>

        <template>
          <a-col>
            <a-input-search v-model="queryParam.title"
                            placeholder="请输入公告标题，内容"
                            @search="search"
                            allowClear />
          </a-col>
        </template>
      </a-row>
    </div>
    <a-table :columns="columns"
             :rowKey="(record, index) => index"
             :data-source="data"
             @change="handleTableChange"
             :pagination="pagination">
      <a @click="handleDetails(record.id)"
         :title="record.title"
         slot="tl"
         slot-scope="text,record">
        {{ text }}
      </a>
      <span slot="action"
            slot-scope="text, record">
        <template v-if="hasPermission('edit')">
          <a @click="handleEdit(record.id)">编辑</a>

        </template>
        <a-divider v-if="hasPermission('delete', 'edit')"
                   type="vertical" />
        <template v-if="hasPermission('delete')">

          <a-popconfirm title="确定要删除吗？"
                        ok-text="是"
                        cancel-text="否"
                        @confirm="handleDelete(record.id)">
            <a href="javascript:;">删除</a>
          </a-popconfirm>
        </template>
      </span>
    </a-table>
  </page-card>
</template>

<script>
import { del, page } from '../../api/announcement/index'
import { getCode } from '../../api/dictionary/index'
import { PAGINATION, TABLE_STATUS_CELL } from '../../constants'

export default {
  data () {
    return {
      pagination: PAGINATION,
      queryParam: {},
      data: [],
      typeList: [],
      columns: [
        {
          title: '标题',
          dataIndex: 'title',
          scopedSlots: { customRender: 'tl' },
          ellipsis: true
        },
        {
          title: '分类',
          dataIndex: 'groupType',
          align: 'center',
          ellipsis: true
        },
        {
          title: '作者',
          width: 150,
          align: 'center',
          dataIndex: 'author',
          ellipsis: true
        },
        {
          title: '创建时间',
          align: 'center',
          width: 200,
          dataIndex: 'createTime',
          customRender: (v) => (v ? v.substring(0, 16) : '')
        },
        {
          title: '有效时间',
          align: 'center',
          width: 200,
          dataIndex: 'deadlineTime',
          customRender: (v) => (v ? v.substring(0, 20) : '')
        }
      ]
    }
  },
  created () {
    if (this.hasPermissions('edit', 'delete')) {
      this.columns.push({
        title: '操作',
        width: 150,
        align: 'center',
        scopedSlots: { customRender: 'action' }
      })
    }
    if (!this.$route.meta.keepAlive) {
      this.load()
    }
  },
  activated () {
    this.getCode()
    this.load()
  },
  methods: {
    getCode () {
      const _this = this
      getCode('ANOUNCEMENT_TYPE').then((response) => {
        _this.typeList = response
      })
    },
    search () {
      this.pagination.current = 1
      this.load()
    },
    load () {
      this.queryParam.currPage = this.pagination.current
      this.queryParam.pageSize = this.pagination.pageSize
      page({ ...this.queryParam }).then((response) => {
        let dataList = []
        response.list.forEach((item) => {
          let type = this.typeList.filter((i) => i.dictKey == item.type)
          if (type.length > 0) {
            item.groupType = type[0].dictValue
          } else {
            item.groupType = '未分类'
          }
          if (item.deadlineTime == null || item.deadlineTime == '') {
            item.deadlineTime = '长期'
          }
          dataList.push(item)
        })
        this.data = dataList
        this.pagination.current = response.currPage
        this.pagination.total = response.totalCount
        this.pagination.pageSize = response.pageSize
      })
    },
    reload () {
      this.load({
        currPage: this.pagination.current,
        pageSize: this.pagination.pageSize,
        queryParam: this.queryParam
      })
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = pagination
      this.load()
    },
    handleEdit (id) {
      this.$router.push({
        path: '/announcement/edit',
        query: { id }
      })
    },
    handleDetails (id) {
      this.$router.push({
        path: '/announcement/detail',
        query: { id }
      })
    },
    handleDelete (id) {
      del(id).then((res) => {
        this.load()
      })
    }
  }
}
</script>

<style>
</style>
